package com.wish.group05.base.service.impl;

import cn.hutool.core.date.DateTime;
import com.wish.group05.base.dto.CwPaymentPlanDto;
import com.wish.group05.base.dto.ResponsePayManageDto;
import com.wish.group05.base.entity.CwPaymentPlan;
import com.wish.group05.base.entity.SfContract;
import com.wish.group05.base.mapper.CwPaymentPlanMapper;
import com.wish.group05.base.service.ICwPaymentPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wish.group05.common.security.utils.SecurityUtils;
import cn.hutool.core.date.DateUtil;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author houzehui
 * @since 2022-05-23
 */
@Service
public class CwPaymentPlanServiceImpl extends ServiceImpl<CwPaymentPlanMapper, CwPaymentPlan> implements ICwPaymentPlanService {

    @Override
    public List<CwPaymentPlan> getPayments(SfContract sfContract) {
        //新建付款单集合
        List<CwPaymentPlan> cwPaymentPlans = new ArrayList<CwPaymentPlan>();
        //获取押金
        BigDecimal depositMoney = sfContract.getDepositMoney();
        //押
        Integer depositType = sfContract.getDepositType();
        //总押金=押金*押X（使用BigDecimal的功能multiply）
        BigDecimal multiply = depositMoney.multiply(new BigDecimal(depositType));
        //月租金
        BigDecimal monthRent = sfContract.getMonthRent();
        //押
        Integer paymentType = sfContract.getPaymentType();
        //租金=Y*月租金
        BigDecimal multiply1 = monthRent.multiply(new BigDecimal(paymentType));
        //获取租约开始时间
        Date leaseStartTime = sfContract.getLeaseStartTime();
        //如果总押金不为空
        if (!multiply.equals(BigDecimal.ZERO)) {
            CwPaymentPlan cwPaymentPlan = new CwPaymentPlan();
            //费用类型：81押金
            cwPaymentPlan.setItemType(81);
            //合同类型"1：收房合同，\n2：出房合同"'
            cwPaymentPlan.setContractType(1);
            //,1:收房付款单 2：租务付款单 3：解约结算付款 4：续约结算付款
            cwPaymentPlan.setPayType(1);
            //"1、未付款；2、已付 3、已中止  4：已冻结
            cwPaymentPlan.setPayState(1);
            //账单类型（金融属性）,"1：收房贷变更 2：退租变更 3：普通账单(正常收房租务计划) 4：续约结算产生的"
            cwPaymentPlan.setBillType(3);
            //款项（费用类型）,1:水费 2：电费，3：煤/燃气费，80：租金，81：押金，90：解约结算，99:其它
            cwPaymentPlan.setItemType(80);
            //应付金额
            cwPaymentPlan.setPaymentAmount(multiply);
            //税费
            cwPaymentPlan.setTax(new BigDecimal(0));
            //序号
            cwPaymentPlan.setPayNumber(0);
            //账单开始日
            cwPaymentPlan.setPaymentDaysBegin(leaseStartTime);
            //应付日期
            cwPaymentPlan.setPayableDate(leaseStartTime);
            //账期截止日
            cwPaymentPlan.setPaymentDaysEnd(DateUtil.offsetDay(DateUtil.offsetMonth(leaseStartTime, 1), -1));
            cwPaymentPlan.setCreateUserid(SecurityUtils.getUserId());
            cwPaymentPlans.add(cwPaymentPlan);
        }
        //租金账单
        for (int i = 1; ; i++) {
            CwPaymentPlan cwPaymentPlan = new CwPaymentPlan();
            //费用类型80租金
            cwPaymentPlan.setItemType(80);
            cwPaymentPlan.setContractType(1);
            cwPaymentPlan.setPayType(1);
            //付款状态
            cwPaymentPlan.setPayState(1);
            cwPaymentPlan.setBillType(3);
            //第几期赋值
            cwPaymentPlan.setPayNumber(i);
            //税费
            cwPaymentPlan.setTax(new BigDecimal(0));
            cwPaymentPlan.setPaymentDaysBegin(leaseStartTime);
            //应付日期
            cwPaymentPlan.setPayableDate(leaseStartTime);
            //开始日期变化
            leaseStartTime = DateUtil.offsetMonth(leaseStartTime, paymentType);
            DateTime dateTime = DateUtil.offsetDay(leaseStartTime, -1);
            cwPaymentPlan.setCreateUserid(SecurityUtils.getUserId());
            //最后一期结束日期>=总结束日期, 结束循环
            if (DateUtil.compare(leaseStartTime, sfContract.getLeaseEndTime()) >= 0) {
                //获取月份差值(不足一个月不计算)
                double floor = Math.floor((DateUtil.dayOfYear(dateTime) - DateUtil.dayOfYear(sfContract.getLeaseEndTime())) / 30.0);
                cwPaymentPlan.setPaymentAmount(monthRent.multiply(new BigDecimal(paymentType - floor)));
                cwPaymentPlan.setPaymentDaysEnd(sfContract.getLeaseEndTime());
                cwPaymentPlans.add(cwPaymentPlan);
                break;
            }
            //应付金额
            cwPaymentPlan.setPaymentAmount(multiply1);
            cwPaymentPlan.setPaymentDaysEnd(dateTime);
            cwPaymentPlans.add(cwPaymentPlan);
        }
        return cwPaymentPlans;
    }

    @Override
    public List<ResponsePayManageDto> getPaymentPlanList(CwPaymentPlanDto cwPaymentPlanDto) {
        List<ResponsePayManageDto> list = getBaseMapper().getPaymentPlanList(cwPaymentPlanDto);
        return list;
    }
}
